//
//  NSString+BluseStr.m
//  he_Lan
//
//  Created by xyyl on 2021/6/21.
//  Copyright © 2021 xyyl. All rights reserved.
//

#import "NSString+BluseStr.h"

@implementation NSString (BluseStr)

//- (NSString *)byChufangDataToSimpleStr {
//    NSDictionary * dict =  [ChuFangModelDefault getAllModelData];
//    if (dict == nil) {
//        return @"worng";
//    }else{
//        NSString * str = dict[self];
//        if (str == nil) {
//            return @"cf wrong";
//        }else{
//            return str;
//        }
//    }
//}

+ (NSArray *)High_lowConversionWith:(NSString *)str {
    if (str.length == 4) {
        NSString * fir = [str substringFromIndex:[str length]-2];
        
        NSString * sec = [str substringToIndex:2];
        NSLog(@"%@%@",fir,sec);
        return @[fir,sec];
    }else{
        return @[];
    }
}

///16进制转10进制
- (NSString *)decimalTohexadecima {
    return [NSString stringWithFormat:@"%lu",strtoul(self.UTF8String, 0, 16)];
}

- (NSInteger)calculatetheAngleplusOrMinus {
    NSInteger  jiaodu = [self integerValue];
    
    return jiaodu > 127 ?(128 - jiaodu) :jiaodu;
}

- (NSString *)hexadecimaTtoDecimalAddZero {
    NSInteger  decimal = [self integerValue];
    NSString *hex =@"";
    NSString *letter;
    NSInteger number;
    for (int i = 0; i<9; i++) {
        
        number = decimal % 16;
        decimal = decimal / 16;
        switch (number) {
                
            case 10:
                letter =@"A"; break;
            case 11:
                letter =@"B"; break;
            case 12:
                letter =@"C"; break;
            case 13:
                letter =@"D"; break;
            case 14:
                letter =@"E"; break;
            case 15:
                letter =@"F"; break;
            default:
                letter = [NSString stringWithFormat:@"%ld", number];
        }
        hex = [letter stringByAppendingString:hex];
        if (decimal == 0) {
            
            break;
        }
    }
    if (hex.length == 1) {
        hex = [NSString stringWithFormat:@"000%@",hex];
    }
    if (hex.length == 3) {
        hex = [NSString stringWithFormat:@"0%@",hex];
    }
    if (hex.length == 2) {
        hex = [NSString stringWithFormat:@"00%@",hex];

    }
    NSLog(@"%@",hex);
    return hex;
}
///10进制转化16进制（计算是几位就几位）
- (NSString *)hexadecimaTtoDecimal {
    NSInteger  decimal = [self integerValue];
    NSString *hex =@"";
    NSString *letter;
    NSInteger number;
    for (int i = 0; i<9; i++) {
        
        number = decimal % 16;
        decimal = decimal / 16;
        switch (number) {
                
            case 10:
                letter =@"A"; break;
            case 11:
                letter =@"B"; break;
            case 12:
                letter =@"C"; break;
            case 13:
                letter =@"D"; break;
            case 14:
                letter =@"E"; break;
            case 15:
                letter =@"F"; break;
            default:
                letter = [NSString stringWithFormat:@"%ld", number];
        }
        hex = [letter stringByAppendingString:hex];
        if (decimal == 0) {
            
            break;
        }
    }
    if (hex.length == 1) {
        hex = [NSString stringWithFormat:@"0%@",hex];
    }
    NSLog(@"%@",hex);
    return hex;
    
}

///字符串转data(蓝牙指令string 转化data需要)
+ (NSData *)convertHexStrToData:(NSString *)str
{
    if (!str || [str length] == 0) {
        return nil;
    }
    
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:20];
    NSRange range;
    if ([str length] % 2 == 0) {
        range = NSMakeRange(0, 2);
    } else {
        range = NSMakeRange(0, 1);
    }
    for (NSInteger i = range.location; i < [str length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [str substringWithRange:range];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
        
        [scanner scanHexInt:&anInt];
        NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
        [hexData appendData:entity];
        
        range.location += range.length;
        range.length = 2;
    }
    return hexData;
}

+ (NSString *)hexadecimaAddWithHexadecimaArray:(NSArray *)array {
    if (array.count == 0) {
        return @"";
    }
    Byte A = '\0';
    for (NSString * str in array) {
        unsigned long num1 = strtoul([str UTF8String],0,16);
        A = A + num1;
    }
    return [NSString stringWithFormat:@"%0X",A];
}

+(NSString *)getMMSSFromSS:(NSString *)totalTime{
    //    NSInteger seconds = totalSeconds % 60;
    //NSInteger minutes = (totalSeconds / 60) % 60;
    //    NSInteger hours = totalSeconds / 3600 % 24;
    //    NSInteger day = totalSeconds / 3600 / 24;
//    NSInteger day=(totalSeconds/(3600*24));
//    NSInteger hour=((totalSeconds-day*24*3600)/3600);
//    NSInteger minute=(totalSeconds-day*24*3600-hour*3600)/60;
//    NSInteger second=totalSeconds-day*24*3600-hour*3600-minute*60;
//    return[NSString stringWithFormat:@"%ld天%ld时%ld分%ld秒",day,hour,minute,second]



    NSInteger seconds = [totalTime integerValue];
    NSString *str_minute = [NSString stringWithFormat:@"%.2ld",seconds/60];
    NSString *str_second = [NSString stringWithFormat:@"%.2ld",seconds%60];
    NSString *str_hour = [NSString stringWithFormat:@"%.2ld",seconds/3600/24];
    NSString *format_time = [NSString stringWithFormat:@"%@:%@",str_minute,str_second];
    return format_time;
}


- (double)recoreMinXWithArray:(NSArray *)array{
    double minValue = [[array valueForKeyPath:@"@min.doubleValue"] doubleValue];
    
    minValue =  minValue * 95 /100;
    if (minValue < -60) {
        minValue = -60;
    }
    if (minValue > 60) {
        minValue = 60;
    }
    
    
    return minValue;

}

- (double)recoreMaxXWithArray:(NSArray *)array {
    double maxValue = [[array valueForKeyPath:@"@max.doubleValue"] doubleValue];

    maxValue =  maxValue * 95 /100;
    if (maxValue < 0) {
        maxValue = 0;
    }
    if (maxValue > 60) {
        maxValue = 60;
    }
    
    return maxValue;
}

@end
